package com.vframework.system.role.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vframework.base.exception.BaseException;
import com.vframework.base.model.BaseMap;
import com.vframework.base.service.impl.BaseServiceImpl;
import com.vframework.system.menu.model.MenuBean;
import com.vframework.system.role.mapper.RoleMapper;
import com.vframework.system.role.model.RoleBean;
import com.vframework.system.role.service.RoleService;

@Service
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    
    public Integer getCount(Map<String, Object> paramMap) throws BaseException{
        return roleMapper.getCount(paramMap);
    }
    
    public List<MenuBean>  getRoleMenuForAddList(String orgId) throws BaseException{
		return roleMapper.getRoleMenuForAddList(orgId);
    }
    
	public List<MenuBean>  getMenuByRole(Map<String, Object> paramMap)
			throws BaseException {
		return roleMapper.getMenuByRole(paramMap);
	}
	
	public List<RoleBean> getRoleList(Map<String, Object> paramMap)
			throws BaseException {
		return roleMapper.getRoleList(paramMap);
	}

	public RoleBean getRoleById(String roleId)
	        throws BaseException {
	    return roleMapper.getRoleById(roleId);
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteRole(Map<String, Object> paramMap) throws BaseException {
		String[] ids = (String[])paramMap.get("ids");
		if(ids == null || ids.length == 0){
			throw new BaseException("No data to operate");
		}
		//删除roleMenu
		roleMapper.deleteRoleMenu(ids);
		//删除角色role
		roleMapper.deleteRole(paramMap);
	}
	
	public void editRole(RoleBean role) throws BaseException {
	    roleMapper.updateRole(role);
		this.setRoleMenu(role);
		if(role == null){throw new BaseException("No data to operate");}
		roleMapper.deleteRoleMenu(role.getRoleId());
    	if(role.getMenuSet()!=null ){
    	    for (MenuBean menuBean : role.getMenuSet()) {
                menuBean.setRoleId(role.getRoleId());
            }
    	    roleMapper.insertRoleMenu(role.getMenuSet());
    	}
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void addRole(RoleBean role) throws BaseException {
		this.setRoleMenu(role);
		if(role == null){throw new BaseException("No data to operate");}
		Map<String,Object> map = new BaseMap(); 
		map.put("cols", "role_id");
		map.put("tableName", "sys_role");
		String roleId = roleMapper.getMaxId(map);
		role.setRoleId(roleId);
		roleMapper.insertRole(role);
		roleMapper.deleteRoleMenu(role.getRoleId());
    	if(role.getMenuSet()!=null ){
    	    for (MenuBean menuBean : role.getMenuSet()) {
    	        menuBean.setRoleId(role.getRoleId());
    	    }
    	    roleMapper.insertRoleMenu(role.getMenuSet());
    	}
	}
	
	private void createMenuBean(Map<String, MenuBean> menuMap, String str, String roleType) {
	    if (menuMap.get(str) == null) {
            menuMap.put(str, dealMenuBean(new MenuBean(), str, roleType));
        } else {
            menuMap.put(str, dealMenuBean(menuMap.get(str), str, roleType));
        }
	}
	
	private MenuBean dealMenuBean(MenuBean menuBean, String str, String roleType) {
        menuBean.setMenuId(str);
        if ("selectRole".equals(roleType)) {
            menuBean.setSelectRole(1);
        } else if ("addRole".equals(roleType)) {
            menuBean.setAddRole(1);
        } else if ("updateRole".equals(roleType)) {
            menuBean.setUpdateRole(1);
        } else if ("deleteRole".equals(roleType)) {
            menuBean.setDeleteRole(1);
        } else if ("importRole".equals(roleType)) {
            menuBean.setImportRole(1);
        } else if ("exportRole".equals(roleType)) {
            menuBean.setExportRole(1);
        }
        return menuBean;
	}
	
	private void setRoleMenu(RoleBean role) {
	    Map<String, MenuBean> menuMap = new HashMap<String, MenuBean>();
	    if (role.getSelectRole() != null && role.getSelectRole().length > 0) {
	        for (String str: role.getSelectRole()) {
	            createMenuBean(menuMap, str, "selectRole");
	        }
	    }
	    if (role.getAddRole() != null && role.getAddRole().length > 0) {
    	    for (String str: role.getAddRole()) {
    	        createMenuBean(menuMap, str, "addRole");
    	    }
	    }
	    if (role.getUpdateRole() != null && role.getUpdateRole().length > 0) {
	        for (String str: role.getUpdateRole()) {
	            createMenuBean(menuMap, str, "updateRole");
	        }
	    }
	    if (role.getDeleteRole() != null && role.getDeleteRole().length > 0) {
	        for (String str: role.getDeleteRole()) {
	            createMenuBean(menuMap, str, "deleteRole");
	        }
	    }
	    if (role.getImportRole() != null && role.getImportRole().length > 0) {
	        for (String str: role.getImportRole()) {
	            createMenuBean(menuMap, str, "importRole");
	        }
	    }
	    if (role.getExportRole() != null && role.getExportRole().length > 0) {
	        for (String str: role.getExportRole()) {
	            createMenuBean(menuMap, str, "exportRole");
	        }
	    }
	    List<MenuBean> menuSet = new ArrayList<MenuBean>();
	    Iterator<Map.Entry<String, MenuBean>> entries = menuMap.entrySet().iterator();
	    while (entries.hasNext()) {  
	        Map.Entry<String, MenuBean> entry = entries.next();
	        MenuBean tempMenuBean = entry.getValue();
	        //设置roleId
	        tempMenuBean.setRoleId(role.getRoleId());
	        menuSet.add(tempMenuBean);
	    }  
		role.setMenuSet(menuSet);
	}
}